package org.ladybug.spring.security;

import java.util.Iterator;
import java.util.List;

import org.springframework.security.AccessDeniedException;
import org.springframework.security.Authentication;
import org.springframework.security.ConfigAttribute;
import org.springframework.security.ConfigAttributeDefinition;
import org.springframework.security.InsufficientAuthenticationException;
import org.springframework.security.vote.AbstractAccessDecisionManager;
import org.springframework.security.vote.AccessDecisionVoter;

public class LadybugAccessDecisionManager extends AbstractAccessDecisionManager {

	public void decide(Authentication authentication, Object object,
			ConfigAttributeDefinition config) throws AccessDeniedException,
			InsufficientAuthenticationException {
		System.out.println("LadybugAccessDecisionManager.decide()");
		System.out.println("Authentication: "+authentication);
		System.out.println("Object: "+object);
		System.out.println("ConfigAttributeDefinition: "+config);
		
		List accessDecVoter = getDecisionVoters();
		
		for (Iterator iterator = accessDecVoter.iterator(); iterator.hasNext();) {
			AccessDecisionVoter voter = (AccessDecisionVoter) iterator.next();
			int result = voter.vote(authentication, object, config);
			switch(result){
			case AccessDecisionVoter.ACCESS_DENIED:
				throw new AccessDeniedException("Accesso negato");
			}
			
		}
		
	}

	public boolean supports(ConfigAttribute attribute) {
		System.out.println("LadybugAccessDecisionManager.supports()");
		System.out.println("ConfigAttribute: "+attribute);
		return true;
	}

	public boolean supports(Class clazz) {
		System.out.println("LadybugAccessDecisionManager.supports()");
		System.out.println(clazz);
		return true;
	}

}
